﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Tools.Base
{
    public class ClipBoard
    {
        [DllImport("user32.dll", SetLastError = true)]
        private static extern Int32 IsClipboardFormatAvailable(uint format);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern Int32 OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetClipboardData(uint uFormat);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern Int32 CloseClipboard();

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern Int32 GlobalLock(IntPtr hMem);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern Int32 GlobalUnlock(IntPtr hMem);

        [DllImport("kernel32.dll")]
        public static extern UIntPtr GlobalSize(IntPtr hMem);

        const uint CF_TEXT = 1;

        /// <summary>
        /// 实现控制台自动粘贴板功能
        /// </summary>
        /// <returns></returns>
        public static string PasteTextFromClipboard()
        {
            string result = "";
            if (IsClipboardFormatAvailable(CF_TEXT) == 0)
            {
                return result;
            }
            if (OpenClipboard((IntPtr)0) == 0)
            {
                return result;
            }

            IntPtr hglb = GetClipboardData(CF_TEXT);
            if (hglb != (IntPtr)0)
            {
                UIntPtr size = GlobalSize(hglb);
                IntPtr s = (IntPtr)GlobalLock(hglb);
                byte[] buffer = new byte[(int)size];
                Marshal.Copy(s, buffer, 0, (int)size);
                if (s != null)
                {
                    result = ASCIIEncoding.ASCII.GetString(buffer);
                    GlobalUnlock(hglb);
                }
            }

            CloseClipboard();
            return result;
        }

        //static string _str = "";
        ///// <summary>
        ///// 监控用户输入的按键
        ///// </summary>
        //public static string KeyPress(out bool bEnter)
        //{
        //    bEnter = false;

        //    ConsoleKeyInfo ki = Console.ReadKey(true);
        //    string s = "";
        //    if ((ki.Key == ConsoleKey.V) && (ki.Modifiers == ConsoleModifiers.Control))
        //    {
        //        s = ClipBoard.PasteTextFromClipboard();
        //    }
        //    else if(ki.Key == ConsoleKey.Enter)
        //    {
        //        bEnter = true;
        //        return _str;
        //    }
        //    else
        //    {
        //        s = ki.KeyChar.ToString();
        //    }


        //    return s;
        //}
    }
}
